#!/usr/bin/python
#Pyngpong by Storm Dragon
#Started on Monday, November 30, 2009
#License GPL Version 2

#import section
from player import *
import ConfigParser
import os
import pygame
import random
import speech
import time
import urllib
import sys
import game
import menu

#functions
def getInput(keyEcho = True):
    text = ""
    while True:
        event = pygame.event.wait()
        if event.type == pygame.KEYDOWN:
            if event.key != pygame.K_BACKSPACE:
                text += event.unicode
                if keyEcho == True: speech.speak(event.unicode)
            if event.key == pygame.K_BACKSPACE:
                if keyEcho == True: speech.speak(text[len(text) - 1])
                text = text[: - 1]
#                text = str(tempString)
            if event.key == pygame.K_RETURN: break
            if event.key == pygame.K_ESCAPE:
                text = ""
                break
    return text

def waitForKeyPress(message):
    message += " Press any key to continue"
    speech.speak(message)
    while True:
        event = pygame.event.wait()
        if event.type == pygame.KEYDOWN: break

def getChoice():
    answer = True
    while True:
        event = pygame.event.wait()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                if answer == True:
                    answer = False
                else:
                    answer = True
            if event.key == pygame.K_y: answer = True
            if event.key == pygame.K_n: answer = False
            if answer == True: speech.speak("Yes")
            if answer == False: speech.speak("No")
            if event.key == pygame.K_RETURN: break
    return answer

def getIdentica(cFile):
    message = "Please enter your Identica user name."
    speech.speak(message)
    identicaName = getInput()
    message = "Please enter your Identica password."
    speech.speak(message)
    identicaPassword = getInput(False)
    config.writeVar(cFile, "identica", "username", identicaName)
    config.writeVar(cFile, "identica", "password", identicaPassword)

def updateStatus(identicaName, identicaPassword, player1Score, player2Score):
    #incode the data for transmition
    data = urllib.urlencode({"status" : name1 + " verses " + name2 + " final score " + str(score1) + " to " + str(score2) + ". #Pyngpong"})
    #Post the data to Identica
    result = urllib.urlopen("http://%s:%s@identi.ca/statuses/update.xml" % (identicaName,identicaPassword), data)
    return result

def coinToss():
    landedOn = random.randint(0, 1)
    game.PlaySound("cointoss")
    time.sleep(flip.get_length())
    if landedOn <= 0:
        return 1
    else:
        return 2

identicaName = getVar(configurationFile, "identica", "username")
identicaPassword = getVar(configurationFile, "identica", "password")

#loop until the game is exited.
while continueGame == True:
    #run pump once per game loop
    pygame.event.pump()

    #game menu
    gameState = gameMenu()
    if gameState == 0:
        #Reset game variables
        player1 = player()
        player2 = player()
        player1.setName(getVar(configurationFile, "settings", "name"))
        while len(player1.getName()) <= 1:
            message = "please enter your name"
            speech.speak(message)
            writeVar(configurationFile, "settings", "name", getInput())
            player1.setName(getVar(configurationFile, "settings", "name"))
        player2.setName("CPU")
        message = player1.getName() + " verses " + player2.getName() + " Here is the coin toss"
        waitForKeyPress(message)
        server = coinToss()
        if server == 1:
            message = player1.getName()
        else:
            message = player2.getName()
        message += " won the cointoss and will get first serve."
        speech.speak(message)
        gameState = gameMenu()
        
        #end game play
    if gameState == 1:
        soundTheme = selectSoundTheme()
        config.writeVar(configurationFile, "settings", "theme", soundTheme)
        gameState = gameMenu()
    if gameState == 2:
        getIdentica(configurationFile)
        identicaName = getVar(configurationFile, "identica", "username")
        identicaPassword = getVar(configurationFile, "identica", "password")
        gameState = gameMenu()
    if gameState == 3:
        continueGame = False
sys.exit(0)

def ExitGame(game):
    speech.speak("Thanks for playing.", True)
    game.running = False
    def selectSoundTheme(game):
    soundIndex = os.listdir("sounds")
    sm = Menu()
    for theme in soundIndex:
        sm.Add((theme, lambda game: game.SetTheme(theme)))
    sm.Show(game, "Sound theme selector menu.")
def PlayGame(game):

if __name__=="__main__":
    game = Game()
    game.LoadIdentica()
    game.LoadTheme()
    game.AddSound("hit")
    game.AddSound("cointoss")
    Menu mm()
    mm.AddItem(("play", PlayGame))
    mm.Add("exit game", ExitGame)
    mm.show(game, "Pyngpong main menu.")
    game.Run()
